//
//  ExaminationPaperListCell.m
//  JPWords
//
//  Created by 梁淑婷 on 2024/6/2.
//

#import "ExaminationPaperListCell.h"

@interface ExaminationPaperListCell()

@property(nonatomic,strong)UIView *bgView;
@property(nonatomic,strong)UIImageView *logoImageView;
@property(nonatomic,strong)UILabel *titleLabel;
@property(nonatomic,strong)UIButton *testBtn;
@property(nonatomic,strong)UILabel *tipLabel;

@end

@implementation ExaminationPaperListCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
        [self createUI];
    }
    return self;
}

-(void)createUI{
    
    __weak typeof(self)self_weak_ = self;
    if(!self.currentAvailableWidth){
        self.currentAvailableWidth = self.width;
    }
    self.backgroundColor = KbgColor;
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.bgView = [[UIView alloc]initWithFrame:CGRectMake(16,0,self.currentAvailableWidth-32,62)];
    self.bgView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:self.bgView];
    self.logoImageView = [[UIImageView alloc]init];
    [self.bgView addSubview:self.logoImageView];
    [self.logoImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(@20);
        make.centerY.mas_equalTo(self_weak_.bgView);
        make.left.mas_equalTo(@16);
    }];
    self.testBtn = [[UIButton alloc]init];
    [self.testBtn setTitle:@"开始测试" forState:UIControlStateNormal];
    [self.testBtn setTitleColor:KThemeColor forState:UIControlStateNormal];
    self.testBtn.titleLabel.font = JPUIFontPingFangSCRegular(14);
    self.testBtn.backgroundColor = UIColorHex_(0x15CC9D,0.1);
    self.testBtn.layer.cornerRadius = 15;
    self.testBtn.layer.masksToBounds = YES;
    [self.testBtn addTarget:self action:@selector(testBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.bgView addSubview:self.testBtn];
    [self.testBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(@-12);
        if(self_weak_.currentAvailableWidth < 375){
            make.width.mas_equalTo(@70);
        }else{
            make.width.mas_equalTo(@80);
        }
        make.height.mas_equalTo(@30);
        make.centerY.mas_equalTo(self_weak_.bgView);
    }];
    self.tipLabel = [[UILabel alloc]init];
    if(self.currentAvailableWidth < 375){
        self.tipLabel.font = JPUIFontPingFangSCRegular(12);
    }else{
        self.tipLabel.font = JPUIFontPingFangSCRegular(14);
    }
    self.tipLabel.textColor = UIColorHex(0xFF781D);
    self.tipLabel.textAlignment = NSTextAlignmentCenter;
    self.tipLabel.layer.cornerRadius = 15;
    self.tipLabel.layer.masksToBounds = YES;
    self.tipLabel.backgroundColor = UIColorHex_(0xFF781D,0.1);
    self.tipLabel.userInteractionEnabled = YES;
    CGFloat tip_w = [self getStringWidthWithText:self.tipLabel.text font:self.tipLabel.font viewHeight:30];
    [self.bgView addSubview:self.tipLabel];
    [self.tipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        if(self_weak_.currentAvailableWidth < 375){
            make.right.mas_equalTo(self_weak_.testBtn.mas_left).offset(-5);
            make.width.mas_equalTo(tip_w+15);
        }else{
            make.right.mas_equalTo(self_weak_.testBtn.mas_left).offset(-16);
            make.width.mas_equalTo(tip_w+24);
        }
        make.height.mas_equalTo(@30);
        make.centerY.mas_equalTo(self_weak_.bgView);
    }];
    self.tipLabel.hidden = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithActionBlock:^(id  _Nonnull sender) {
        if (self_weak_.delegate && [self_weak_.delegate respondsToSelector:@selector(correctRateTap:)]){
            [self_weak_.delegate correctRateTap:self_weak_];
        }
    }];
    [self.tipLabel addGestureRecognizer:tap];
    
    self.titleLabel = [[UILabel alloc]init];
    if(self.currentAvailableWidth < 375){
        self.titleLabel.font = JPUIFontPingFangSCMedium(14);
    }else{
        self.titleLabel.font = JPUIFontPingFangSCMedium(15);
    }
    self.titleLabel.textColor = KFontBlackColor;
    [self.bgView addSubview:self.titleLabel];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self_weak_.logoImageView.mas_right).offset(12);
        make.height.mas_equalTo(@21);
        make.centerY.mas_equalTo(self_weak_.bgView);
        if(self_weak_.currentAvailableWidth < 375){
            make.right.mas_equalTo(self_weak_.testBtn.mas_left).offset(-tip_w-15-20);
        }else{
            make.right.mas_equalTo(self_weak_.testBtn.mas_left).offset(-tip_w-24-28);
        }
    }];
    
}


-(void)refreshWithIsBottom:(BOOL)isBottom andIsTop:(BOOL)isTop{
    self.bgView.frame =  CGRectMake(16,0,self.currentAvailableWidth-32,62);
    if(isBottom == YES){
        [self.bgView addRoundedCorners:UIRectCornerBottomLeft|UIRectCornerBottomRight withRadii:CGSizeMake(10,10)];
    }else if (isTop == YES){
        [self.bgView addRoundedCorners:UIRectCornerTopLeft|UIRectCornerTopRight withRadii:CGSizeMake(10,10)];
    }else{
        [self.bgView addRoundedCorners:UIRectCornerAllCorners withRadii:CGSizeMake(0,0)];
    }
    
}

-(void)setModel:(ExamPaperTypeListModel *)model{
    _model = model;
    self.titleLabel.text = model.typeName;
    switch (model.typeId.integerValue) {
        case 33:
            //听力
            self.logoImageView.image = [UIImage imageNamed:@"exam_test_list_listen"];
            break;
        case 34:
            //单选
            self.logoImageView.image = [UIImage imageNamed:@"exam_test_list_choice"];
            break;
        case 35:
            //阅读
            self.logoImageView.image = [UIImage imageNamed:@"exam_test_list_read"];
            break;
        case 36:
            //写作
            self.logoImageView.image = [UIImage imageNamed:@"exam_test_list_write"];
            break;
        case 37:
            //基础知识
            self.logoImageView.image = [UIImage imageNamed:@"exam_test_list_basic"];
            break;
        case 45:
            //文字词汇
            self.logoImageView.image = [UIImage imageNamed:@"exam_test_list_word"];
            break;
        case 46:
            //语法
            self.logoImageView.image = [UIImage imageNamed:@"exam_test_list_grammar"];
            break;
        case 56:
            //阅读A
            self.logoImageView.image = [UIImage imageNamed:@"exam_test_list_read"];
            break;
        case 55:
            //阅读B
            self.logoImageView.image = [UIImage imageNamed:@"exam_test_list_read"];
            break;
        default:
            break;
    }
    
    __weak typeof(self)self_weak_ = self;
    [self.testBtn mas_updateConstraints:^(MASConstraintMaker *make) {
        if(self_weak_.currentAvailableWidth < 375){
            make.width.mas_equalTo(@70);
        }else{
            make.width.mas_equalTo(@80);
        }
    }];
    if(self.currentAvailableWidth < 375){
        self.tipLabel.font = JPUIFontPingFangSCRegular(12);
    }else{
        self.tipLabel.font = JPUIFontPingFangSCRegular(14);
    }
    if(self.currentAvailableWidth < 375){
        self.titleLabel.font = JPUIFontPingFangSCMedium(14);
    }else{
        self.titleLabel.font = JPUIFontPingFangSCMedium(15);
    }
    if(model.typeId.integerValue == 36){
        //写作
        self.tipLabel.hidden = YES;
        [self.testBtn setTitle:@"查看范文" forState:UIControlStateNormal];
        [self.titleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(self_weak_.testBtn.mas_left).offset(-12);
        }];
    }else if(model.typeId.integerValue == 55){
        //阅读B
        self.tipLabel.hidden = YES;
        [self.testBtn setTitle:@"查看翻译" forState:UIControlStateNormal];
        [self.titleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(self_weak_.testBtn.mas_left).offset(-12);
        }];
    }else{
        if([model.isFinish isEqualToString:@"1"]){
            [self.testBtn setTitle:@"重新测试" forState:UIControlStateNormal];
            self.tipLabel.hidden = NO;
            self.tipLabel.text =  [NSString stringWithFormat:@"正确率%@%%",model.correctPercent];
            CGFloat tip_w = [self getStringWidthWithText:self.tipLabel.text font:self.tipLabel.font viewHeight:30];
            [self.tipLabel mas_updateConstraints:^(MASConstraintMaker *make) {
                if(self_weak_.currentAvailableWidth < 375){
                    make.right.mas_equalTo(self_weak_.testBtn.mas_left).offset(-5);
                    make.width.mas_equalTo(tip_w+15);
                }else{
                    make.right.mas_equalTo(self_weak_.testBtn.mas_left).offset(-16);
                    make.width.mas_equalTo(tip_w+24);
                }
            }];
            [self.titleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
                if(self_weak_.currentAvailableWidth < 375){
                    make.right.mas_equalTo(self_weak_.testBtn.mas_left).offset(-tip_w-15-20);
                }else{
                    make.right.mas_equalTo(self_weak_.testBtn.mas_left).offset(-tip_w-24-28);
                }
            }];
        }else{
            [self.testBtn setTitle:@"开始测试" forState:UIControlStateNormal];
            self.tipLabel.hidden = YES;
            [self.titleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
                make.right.mas_equalTo(self_weak_.testBtn.mas_left).offset(-12);
            }];
        }
    }
}


#pragma mark - 开始测试
-(void)testBtnClick{
    if (self.delegate && [self.delegate respondsToSelector:@selector(optionTestTap:)]){
        [self.delegate optionTestTap:self];
    }
}


@end
